<template>
  <div class="employee-header">
    <div class="employee-left">
      <i class="el-icon-menu" @click="sendParameter"></i>
      <!-- <i class="el-icon-refresh"></i> -->
    </div>
    <div class="employee-right">
      <span class="nickname">{{ userForm.nickname }}</span>
      <el-dropdown @command="handleCommand" style="height: 30px">
        <el-avatar :size="30" :src="imageUrl"></el-avatar>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="1" v-if="role === 'employee'">个人中心</el-dropdown-item>
          <el-dropdown-item command="2" v-if="role === 'employee'">修改密码</el-dropdown-item>
          <el-dropdown-item command="/manage/login">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="28%" :before-close="handleClose">
      <template v-if="dialogTag === 1">
        <el-form size="mini" label-width="60px">
          <el-form-item label-width="0">
<!-- <<<<<<< HEAD

            <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">

              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
======= -->
            <el-upload class="avatar-uploader" :action="updateAvater" :show-file-list="false"
              :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="userForm.imgUrl" :src="userForm.imgUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="userForm.nickname"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="userForm.sex" placeholder="请选择" style="width: 100%;">
              <el-option value="1" label="男"></el-option>
              <el-option value="0" label="女"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="userForm.age" type="number"></el-input>
          </el-form-item>
          <!-- <el-form-item label="状态">
            <el-input></el-input>
          </el-form-item> -->
          <el-form-item label="手机号">
            <el-input type="number" v-model="userForm.telephone"></el-input>
          </el-form-item>
          <el-form-item label="身份证">
            <el-input v-model="userForm.idCard" type="number"></el-input>
          </el-form-item>
          <el-form-item label-width="0">
            <el-button @click="goSave" type="primary">确认</el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template v-else>
        <el-form size="mini" label-width="70px">
          <el-form-item label="邮箱">
            <el-input v-model="updateForm.email" disabled></el-input>
          </el-form-item>
          <el-form-item label="新密码">
            <el-input v-model="updateForm.password" type="password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input v-model="updateForm.repassword" type="password"></el-input>
          </el-form-item>
          <el-form-item label-width="0">
            <el-col :span="7">
              <el-button @click="sendCode()">发送验证码</el-button>
            </el-col>
            <el-col :span="17">
              <el-input placeholder="请输入验证码" v-model="updateForm.vcode"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label-width="0">
            <el-button type="primary" @click="goUpdate">确认</el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getUpdateCode } from '@/api/common'
import { getMeInfo, updateEmployee, updateEmployeePassword, logout as employeeLogout } from '@/api/employee'
import { logout, meInfo } from '@/api/admin'
export default {
  props: ['title'],
  components: {},
  data () {
    return {
      updateAvater: '',
      isCollapse: false,
      dialogVisible: false,
      dialogTitle: '',
      dialogTag: 1,
      userForm: {},
      imageUrl: '',
      role: '',
      updateForm: {
        email: '',
        password: '',
        repassword: '',
        vcode: ''
      },
      updateRules: {
        email: [
          {
            required: true,
            message: '请输入邮箱地址',
            triggle: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入新密码',
            triggle: 'blur'
          },
          {
            min: 6,
            max: 16,
            message: '密码长度应该为6-16位',
            triggle: 'blur'
          }
        ],
        repassword: [
          {
            required: true,
            message: '请输入确认密码',
            triggle: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请再次输入密码'))
              } else if (value !== this.updateForm.password) {
                callback(new Error('两次输入密码不一致!'))
              } else {
                callback()
              }
            },
            triggle: 'blur'
          }
        ],
        vcode: [
          {
            required: true,
            message: '请输入邮箱验证码',
            triggle: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleCollapse() {
      this.isCollapse = !this.isCollapse
      this.$emit('collapse', this.isCollapse)
    },
    handleDialog(tag) {
      this.dialogTag = tag
      if (tag === 1) {
      }
    }
  },
  created() {
    if (this.$route.path.includes('employee')) {
      this.role = 'employee'
      getMeInfo().then(res => {
        this.userForm = res
        this.updateForm.email = res.email
        if (this.userForm.imgUrl) {
          this.imageUrl = this.userForm.imgUrl
        } else {
          this.imageUrl = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
        }
      })
      this.updateAvater = process.env.VUE_APP_BASE_URL + '/employees/upload'
    } else if (this.$route.path.includes('admin')) {
      this.role = 'admin'
      meInfo().then(res => {
        this.userForm.nickname = res.username
        this.imageUrl = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
      })
    }
  },
  methods: {
    sendParameter() {
      this.isCollapse = !this.isCollapse
      this.$emit('isCollapse', this.isCollapse)
    },
    handleCommand (path) {
      console.log(path)
      if (path === '1' || path === '2') {
        if (path === '1') {
          this.dialogTitle = '个人中心'
          this.dialogTag = 1
        } else {
          this.dialogTitle = '修改密码'
          this.dialogTag = 2
        }
        this.dialogVisible = true
      } else {
        this.$router.push(path)
      }
    },
    async handleAvatarSuccess(file) {
      console.warn(file)
      this.userForm.imgUrl = file.data
      await this.$store.dispatch('user/getUserInfo')
    },
    beforeAvatarUpload() { },
    goSave() {
      updateEmployee(this.userForm).then(res => {
        this.$notify({
          title: '成功',
          message: '更新信息成功！',
          type: 'success'
        })
      }).catch(err => {
        this.$message.error('更新信息失败！')
      })
    },
    updatePassword() {
      updateEmployeePassword(this.userForm).then(res => {
        this.$notify({
          title: '成功',
          message: '密码更新成功！',
          type: 'success'
        })
      }).catch(err => {
        this.$message.error('密码更新失败')
      })
    },
    handleClose() {
      this.dialogVisible = false
    },
    sendCode() {
      this.getUpdateCode()
    },
    async getUpdateCode() {
      console.warn("res")
      console.warn(this.updateForm)
      const res = await getUpdateCode(this.updateForm.email)
      console.warn(res)
      this.$message({
        type: 'success',
        message: res
      })
    },
    async goUpdate() {
      console.error("res")
      console.error(this.updateForm)
      const res = await updateEmployeePassword(this.updateForm)
      this.$message({
        type: 'success',
        message: res
      })
      this.$router.back()
    }
  }
}
</script>

<style lang="less" scoped>
.employee-header {
  position: relative;
  height: 58px;
  text-align: center;
  line-height: 50px;
  background: url(../../assets/big-title.jpg) no-repeat center;
  background-size: contain;
  border-bottom: 2px solid #f3f3f3;
}

.employee-left {
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translate(0, -50%);

  i {
    margin: 0 5px;
  }
}

.employee-right {
  .nickname {
    font-size: 14px;
    font-weight: 700;
    margin-right: 10px;
  }

  display: flex;
  align-items: center;
  height: 30px;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 40px;
  // background-color: red;
}

.dialog-footer {
  display: flex;
  justify-content: center;
}

el-dialog {
  height: 300px;
}

el-dialog {
  height: 300px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
  border-radius: 50%;
}
</style>
